<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <!--<a-col :span="24">-->
            <!--<a-form-model-item label="模板ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sjcjmbId">-->
              <!--<a-input v-model="model.sjcjmbId"placeholder="请输入模板ID" ></a-input>-->
            <!--</a-form-model-item>-->
          <!--</a-col>-->
          <a-col :span="24">
            <a-form-model-item label="数据项名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sjxmc">
              <a-input v-model="model.sjxmc"placeholder="请输入数据项名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物理字段名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="wlzdmc">
              <a-input v-model="model.wlzdmc"placeholder="请输入物理字段名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sjlx">
              <!--<a-input v-model="model.sjlx"placeholder="请输入数据类型" ></a-input>-->
              <a-select default-value="varchar" v-model="model.sjlx" @change="Dates">
                <a-select-option value="varchar">字符型</a-select-option>
                <a-select-option value="int">整型</a-select-option>
                <a-select-option value="double">浮点型</a-select-option>
                <a-select-option value="date">日期型</a-select-option>
                <a-select-option value="tinyint">布尔型</a-select-option>
                <a-select-option value="decimal">精准数据类型</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sjcd">
              <a-input-number v-if="this.boolDate === false" v-model="model.sjcd"placeholder="请输入数据长度" style="width: 501px"></a-input-number>
              <a-input-number v-else v-model="model.sjcd"placeholder="请输入数据长度" style="width: 501px" disabled></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="数据源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sjy">
              <a-textarea :rows="6" v-model="model.sjy" placeholder="请输入数据源" ></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否非空" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sffk">
              <!--<a-input-number v-model="model.sffk"placeholder="请输入是否非空" style="width: 100%" />-->
              <j-switch v-model="model.sffk" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sfqy">
              <j-switch v-model="model.sfqy" :options="[1,0]" ></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="pxh">
              <!--<a-input v-model="model.pxh" placeholder="请输入排序号"></a-input>-->
              <a-input-number v-model="model.pxh" placeholder="请输入排序号" style="width: 200px"></a-input-number>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import ATextarea from "ant-design-vue/es/input/TextArea";

  export default {
    name: "YlshSjcjmbsjxModal",
    components: {
      ATextarea
    },
    props:{
      mainId:{
        type:String,
        required:false,
        default:''
      }
    },
    data () {
      var checkSjcd = (rule, value, callback) => {
        var value = parseInt(value);;
        if (value==="" || value===null) {
          return callback(new Error('请输入数据长度'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value) || (value < 0 || value > 255)) {
            callback(new Error('请输入0~255的数字'));
          } else {
            callback();
          }
        }, 0);
      };
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
          sjlx:"varchar"
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          sjxmc: [{ required: true, message: '请输入数据项名称!' }],
          wlzdmc: [{ required: true, message: '请输入物理字段名称!' }],
          pxh: [{ required: true, message: '请输入排序号!' }],
          sjcd: [
            // { required: true, message: '请输入数据长度!' },
            // { min: 0, max: 255, message: '请输入数据长度', trigger: 'blur' }
            { required: true, validator: checkSjcd, trigger: 'blur' }
            ]
        },
        url: {
          add: "/sjcjmb/ylshSjcjmb/addYlshSjcjmbsjx",
          edit: "/sjcjmb/ylshSjcjmb/editYlshSjcjmbsjx",
        },
        boolDate: false
      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      Dates(){
        if (this.model.sjlx==='tinyint'){
          this.boolDate = true
          this.model.sjcd = 1
        }else {
          this.boolDate = false
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model['sjcjmbId'] = this.mainId
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>
